import useDictStore from '@/store/modules/dict'
import { getDict } from '@/api/system/dict.js'

/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((item, index) => {
      const dictType = item[0];
      const isAppendAllStatus = item[1];
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getDict(dictType).then(dictData => {
          if (isAppendAllStatus) {
            const allObj = {value: '', description: '全部'}
            dictData.unshift(allObj)
          }
          res.value[dictType] = dictData.map(p => ({ label: p.description, value: p.value }))
        })
      }
    })
    return toRefs(res.value);
  })()
}